<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repos\ItemRepo;
use App\Exports\ItemsExport;
use Maatwebsite\Excel\Facades\Excel;

class ItemFeeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = $param = [];
        if($request->contacter) {
            $where['contacter'] = trim($request->contacter);
        }

        if($request->code) {
            $where['code'] = trim($request->code);
        }

        if($request->ship_name) {
            $where['ship_name'] = trim($request->ship_name);
        }

        if($request->ship_line) {
            $where['ship_line'] = trim($request->ship_line);
        }

        if($request->start_date) {
            $where['start_date'] = $request->start_date.' 0:0:0';
            $where['end_date']   = $request->end_date.' 23:59:59';
            $param['start_date'] = $request->start_date;
            $param['end_date']   = $request->end_date;
        }
        if($request->money_type=='receive'){
            $where['money_type'] = 'receive';
        }
        if($request->money_type=='pay'){
            $where['money_type'] = 'pay';
        }
        if($request->money_type=='received'){
            $where['receive_status'] = 1;
            $where['money_type'] = 'received';

        }
        if($request->money_type=='paied'){
            $where['pay_status'] = 1;
            $where['money_type'] = 'paied';

        }
        $list = ItemRepo::feePages($where);
        $type['receive_money'] = ItemRepo::totalByType($where,'receive_money');
        $type['pay_money']     = ItemRepo::totalByType($where,'pay_money');
        
        return view('hrs.fee.index',compact('list','where','type','param'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['status'] = 'warning';
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token']);
       // dd($data);
        $flag = ItemRepo::saveFee($data);
        if($flag['status']) {
            $rs['status'] = 'success';
            $rs['msg']    = '操作成功';
            return back()->with('rs',$rs);
        }
        $rs['msg'] = $flag['msg'];
        return back()->with('rs',$rs)->withInput();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = ItemRepo::feeFind(['id'=>$id]);
        return view('hrs.fee.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $data = $request->except(['_method','_token']);
        $data['username']   = session('admin')->username;
        $data['pay_at']     = date('Y-m-d H:i:s');
        $data['receive_at'] = date('Y-m-d H:i:s');
       
        $where['id'] = $id;
        $flag = ItemRepo::feeUpdata($where,$data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg']    = '操作成功';
            return redirect('items/'.$data['item_id'].'/edit')->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'warning';
        $rs['msg']    = '操作失败';
        $flag = ItemRepo::feeDelete($id);;
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        }
        return back()->with('rs',$rs);
        
    }


    public function changeStatus(Request $request)
    {
        $rs['status'] = false;
        $where['id'] = $request->id;

        if($request->type == 'pay') {
            $data['pay_status'] = $request->status; 
            if($request->status == 1) {
                
                $data['pay_at'] = date('Y-m-d H:i:s');   
            }
            
        } else {
            $data['receive_status'] = $request->status;  
            if($request->status==1) {
                $data['receive_at'] = date('Y-m-d H:i:s');
                 
            }
            
        }
        $data['username'] = session('admin')->username;
        $flag = ItemRepo::feeUpdata($where,$data);
        if($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        return response()->json($rs);

    }

    public function export(Request $request)
    {
        $where = $request->all();
        if($request->start_date) {
            $where['start_date'] = $request->start_date.' 00:00:00';
            $where['end_date']   = $request->end_date.' 23:59:59';
        }
        
        if($request->money_type == 'pay') {
            $type = '应付';
        } else if($request->money_type=='paied') {
            $type = '已付';
        } else if($request->money_type == 'received') {
            $type = '已收';
        } else {
           $type = '应收'; 
        }
        //dd($where);
        return Excel::download(new ItemsExport($where), $type.'费用信息.xlsx');
    }

}
